package com.example.opengldemo.simpleGame.addRigidBody;

import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.collision.shapes.CompoundShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;
import com.example.opengldemo.simpleGame.object.BNAbstractDoll;
import com.example.opengldemo.simpleGame.object.LoadedObjectVertexNormalTexture;
import com.example.opengldemo.simpleGame.util.RigidBodyHelper;

import javax.vecmath.Vector3f;

import static com.example.opengldemo.simpleGame.constant.SourceConstant.*;

public class Robot  extends BNAbstractDoll {
	int texId;
	DiscreteDynamicsWorld dynamicsWorld;
	Vector3f position;
	
	CollisionShape[] robotshape=new CollisionShape[6];
	
	CollisionShape Robotz;
	public Robot(int texId, DiscreteDynamicsWorld dynamicsWorld,
				 LoadedObjectVertexNormalTexture lovo, Vector3f position, int bianhao){
		this.texId=texId;
		this.dynamicsWorld=dynamicsWorld;
		this.lovo=lovo;
		this.position=position;
		this.bianhao=bianhao;
		initRigidBodys();
	}
	public void initRigidBodys()
	{
		robotshape[0]=new BoxShape(new Vector3f(robotfootx,robotfooty,robotfootz));
		robotshape[1]=new BoxShape(new Vector3f(robottuix,robottuiy,robottuiz));
		robotshape[2]=new BoxShape(new Vector3f(robotbodyx,robotbodyy,robotbodyz));
		robotshape[3]=new BoxShape(new Vector3f(robottopx,robottopy,robottopz));
		robotshape[4]=new BoxShape(new Vector3f(robothand1x,robothand1y,robothand1z));
		robotshape[5]=new BoxShape(new Vector3f(robothand2x,robothand2y,robothand2y));
		Robotz=addChild(robotshape);
		
		RigidBodydoll= RigidBodyHelper.addRigidBody(1,Robotz,position.x,position.y,position.z,dynamicsWorld,false);
	
	}
  	public CompoundShape addChild(CollisionShape[] shape)
  	{
  		CompoundShape comShape=new CompoundShape();
  		Transform localTransform = new Transform();

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(robotfootx+robotfooty/4,-robotfooty-robottuiy*2-robotbodyy,0));//���ñ任�����
  		comShape.addChildShape(localTransform, shape[0]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(-robotfootx-robotfooty/4,-robotfooty-robottuiy*2-robotbodyy,0));//���ñ任�����
  		comShape.addChildShape(localTransform, shape[0]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(robotfootx,-robottuiy-robotbodyy,0));
  		comShape.addChildShape(localTransform, shape[1]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(-robotfootx,-robottuiy-robotbodyy,0));
  		comShape.addChildShape(localTransform, shape[1]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(0,0,0));
  		comShape.addChildShape(localTransform, shape[2]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(0,robotbodyy+robottopy,0));
  		comShape.addChildShape(localTransform, shape[3]);


  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(robotbodyx+robothand1x*2,0,0));
  		comShape.addChildShape(localTransform, shape[4]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(robotbodyx+robothand1x*2,-robotbodyy+robothand2y,robothand1z+robothand2z));//���ñ任�����
  		comShape.addChildShape(localTransform, shape[5]);


  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(-robotbodyx-robothand1x*2,0,0));
  		comShape.addChildShape(localTransform, shape[4]);

  		localTransform.setIdentity();
  		localTransform.origin.set(new Vector3f(-robotbodyx-robothand1x*2,-robotbodyy+robothand2y,robothand1z+robothand2z));//���ñ任�����
  		comShape.addChildShape(localTransform, shape[5]);
  		
  		return comShape;
  	}
  	public void drawSelf()
	{
  		    MatrixState3D.pushMatrix();
			Transform trans=RigidBodydoll.getMotionState().getWorldTransform(new Transform());
			MatrixState3D.translate(trans.origin.x,trans.origin.y-speed, trans.origin.z);
			trans.getOpenGLMatrix(MatrixState3D.getMMatrix());
			
			MatrixState3D.pushMatrix();
			MatrixState3D.scale(robotbl,robotbl,robotbl);
			lovo.drawSelf(texId);
			MatrixState3D.popMatrix();

			MatrixState3D.popMatrix();
  		
	}
}
